package com.baichen;

/**
 * AOP通知类型演示
 */
public class AopDemo {
    
    public static void main(String[] args) throws Exception {
        System.out.println("=== AOP 通知类型演示 ===\n");
        
        // 创建原始的实现对象
        MyInterface originalTarget = MyInterfaceFactory.createProxyInstance(new NameHandler());
        
        // 1. Before通知演示
        System.out.println("1. Before通知演示:");
        System.out.println("------------------------");
        MyInterface beforeProxy = MyInterfaceFactory.createProxyInstance(
            new BeforeHandler(originalTarget, "System.out.println(\"[Before] 准备调用方法\");")
        );
        beforeProxy.func1();
        beforeProxy.func2();
        System.out.println();
        
        // 2. After通知演示
        System.out.println("2. After通知演示:");
        System.out.println("------------------------");
        MyInterface afterProxy = MyInterfaceFactory.createProxyInstance(
            new AfterHandler(originalTarget, "System.out.println(\"[After] 方法调用完成\");")
        );
        afterProxy.func1();
        afterProxy.func2();
        System.out.println();
        
        // 3. Around通知演示 - 正常执行
        System.out.println("3. Around通知演示 - 正常执行:");
        System.out.println("------------------------");
        MyInterface aroundProxy = MyInterfaceFactory.createProxyInstance(
            new AroundHandler(originalTarget)
        );
        aroundProxy.func1();
        aroundProxy.func2();
        System.out.println();
        
        // 4. Around通知演示 - 跳过目标方法
        System.out.println("4. Around通知演示 - 跳过目标方法:");
        System.out.println("------------------------");
        MyInterface aroundSkipProxy = MyInterfaceFactory.createProxyInstance(
            new AroundHandler(originalTarget, false)
        );
        aroundSkipProxy.func1();
        aroundSkipProxy.func2();
        System.out.println();
        
        // 5. 组合使用演示 - Before + After
        System.out.println("5. 组合使用演示 - Before + After:");
        System.out.println("------------------------");
        MyInterface beforeTarget = MyInterfaceFactory.createProxyInstance(
            new BeforeHandler(originalTarget, "System.out.println(\"[Before] 前置处理\");")
        );
        MyInterface combinedProxy = MyInterfaceFactory.createProxyInstance(
            new AfterHandler(beforeTarget, "System.out.println(\"[After] 后置处理\");")
        );
        combinedProxy.func1();
        System.out.println();
        
        // 6. 自定义逻辑演示
        System.out.println("6. 自定义逻辑演示:");
        System.out.println("------------------------");
        MyInterface customProxy = MyInterfaceFactory.createProxyInstance(
            new AroundHandler(originalTarget, 
                "System.out.println(\"🚀 开始执行业务逻辑\");",
                "System.out.println(\"✅ 业务逻辑执行完成\");",
                "System.out.println(\"❌ 业务逻辑执行失败: \" + e.getMessage());",
                true)
        );
        customProxy.func3();
        
        // 7. 异常处理演示
        System.out.println("\n7. 异常处理演示:");
        System.out.println("------------------------");
        MyInterface exceptionTarget = MyInterfaceFactory.createProxyInstance(new ExceptionHandler());
        MyInterface exceptionProxy = MyInterfaceFactory.createProxyInstance(
            new AroundHandler(exceptionTarget,
                "System.out.println(\"[Around] 尝试执行可能失败的方法\");",
                "System.out.println(\"[Around] 方法执行成功\");",
                "System.out.println(\"[Around] 捕获到异常: \" + e.getMessage());",
                true)
        );
        
        try {
            exceptionProxy.func1();
        } catch (Exception e) {
            System.out.println("主程序捕获到异常: " + e.getMessage());
        }
        
        System.out.println("\n=== 演示完成 ===");
    }
} 